package com.javablog.elasticsearch;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@SpringBootApplication
@RestController
@Slf4j
public class SearchServiceApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(SearchServiceApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SearchServiceApplication.class);
    }

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    private String indexName = "sms-logs-index";
    @RequestMapping("cc")
    public Object get(String condition){
        String searchAfter = null;
        Object[] objects = new Object[]{};
        if (StringUtils.isNotEmpty(condition)){
            objects=new Object[]{condition};
        }
        AtomicInteger atomicInteger = new AtomicInteger(0);
        try {
//            for (int i = 0; i < 300; i++) {
                SearchRequest searchRequest = new SearchRequest(indexName);
                SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
                searchSourceBuilder.query(QueryBuilders.matchQuery("smsContent", "途虎"));
                searchSourceBuilder.size(1000);
                searchSourceBuilder.from(0);
                searchSourceBuilder.sort("createDate", SortOrder.ASC);
                if (objects.length > 0) {
                    searchSourceBuilder.searchAfter(objects);
                }
                searchRequest.source(searchSourceBuilder);
                SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
                SearchHit[] hits = searchResponse.getHits().getHits();
                if (hits.length!=0){
                    for (SearchHit hit : hits) {
                        atomicInteger.incrementAndGet();
                        log.info(hit.getSourceAsString());
                    }
                    objects = hits[hits.length - 1].getSortValues();
                    log.info("统一计数为:{}",atomicInteger.get());
                    return objects;
                }

//            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




}
